/**
 * Definition for a binary tree node.
 * function TreeNode(val, left, right) {
 *     this.val = (val===undefined ? 0 : val)
 *     this.left = (left===undefined ? null : left)
 *     this.right = (right===undefined ? null : right)
 * }
 */
/**
 * @param {TreeNode} root
 * @return {number}
 */
export var sumRootToLeaf = function (root) {
  let ans = 0
  function dfs(node, path = 0) {
    if (!node) return
    path = (path << 1) | node.val
    if (!node.left && !node.right) ans += path
    dfs(node.left, path)
    dfs(node.right, path)
  }
  dfs(root)
  return ans
}
